package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.exception.BusinessException;
import com.itheima.mapper.ClazzMapper;
import com.itheima.pojo.*;
import com.itheima.service.ClazzService;
import com.itheima.service.EmpService;
import com.itheima.service.StudentService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@Service
public class ClazzServiceImpl extends ServiceImpl<ClazzMapper, Clazz> implements ClazzService {
    @Resource
    private EmpService empService;

    @Resource
    private StudentService studentService;
    @Override
    public PageResult<Clazz> pageClazz(String name, LocalDate begin, LocalDate end, int page, int pageSize) {
        Page<Clazz> pageClazz =  Page.of(page, pageSize);
        pageClazz.addOrder(OrderItem.desc("create_time"));
        pageClazz = lambdaQuery().like(name != null && !name.isEmpty(), Clazz::getName, name)
                .ge(begin != null, Clazz::getBeginDate, begin)
                .le(end != null, Clazz::getEndDate, end).page(pageClazz);
        //获取班主任
        List<Clazz> clazzList = pageClazz.getRecords();
        //获取班主任id集合
        List<Integer> list = clazzList.stream().map(clazz -> {
            return clazz.getMasterId();
        }).toList();
        if (!list.isEmpty()){
            //查询班主任信息
            List<Emp> emps = empService.listByIds(list);
            clazzList.forEach(clazz->{
                emps.forEach(emp -> {
                    if (clazz.getMasterId() != null && clazz.getMasterId().equals(emp.getId())) {
                        clazz.setMasterName(emp.getName());
                    }
                });
            });
        }
        //获取当前时间
        LocalDate today = LocalDate.now();
        //获取班级状态
        clazzList.forEach(clazz -> {
            LocalDate beginDate = clazz.getBeginDate();
            LocalDate endDate = clazz.getEndDate();
            if (beginDate==null || endDate==null){
                clazz.setStatus("未开课");
                return;
            }
            if (today.isAfter(endDate)){
                clazz.setStatus("已结束");
            }else  if (today.isBefore(beginDate)){
                clazz.setStatus("未开始");
            }else {
                clazz.setStatus("进行中");
            }
        });
        return new PageResult<>(pageClazz.getTotal(),pageClazz.getRecords());
    }

    /**
     * 新增班级
     * @param clazz
     */
    @Override
    public void saveClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        clazz.setCreateTime(LocalDateTime.now());
        save(clazz);
    }

    @Override
    public Clazz getClazzBYId(Integer id) {
        //查询班级信息
        Clazz clazz = getById(id);
        //获取id
        Integer masterId = clazz.getMasterId();
        //查询班主任名字
        if (masterId!=null){
            Emp emp = empService.getById(masterId);
            clazz.setMasterName(emp.getName());
        }

        return clazz;
    }

    @Override
    public void updateClazz(Clazz clazz) {
        clazz.setUpdateTime(LocalDateTime.now());
        updateById(clazz);
//        //删除班主任
//        empService.removeById(new LambdaQueryWrapper<Emp>().eq(Emp::getId,clazz.getMasterId()));
//        //新增班主任
//        Integer masterId = clazz.getMasterId();
//        empService.getById(masterId);

    }

    @Override
    public void delete(Integer id) {
        //先查询班级是否有学生
        List<Student> studentList = studentService.list(new LambdaQueryWrapper<Student>().eq(Student::getClazzId, id));
        if (studentList!=null && !studentList.isEmpty()){
            //有学生不能删除
           throw new BusinessException("班级里有学生不能删除");
        }
        //删除
        removeById(id);
    }
}
